package com.bw.user.mvvm.viewmodel;

import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.greendaomoudle.green.DaoCRUDImpl;
import com.bw.greendaomoudle.green.DaoSessionUtils;
import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.filed.UIFiled;
import com.bw.mvvm_comom.mvvm.BaseViewModel;
import com.bw.user.mvvm.model.LoginModel;
import com.bw.user.mvvm.model.entity.UserDaoEntity;
import com.bw.user.mvvm.model.entity.UserEntity;
import com.bw.user.mvvm.model.entity.UserEventEntity;
import com.bw.user.mvvm.view.activity.ForgetActivity;
import com.bw.user.mvvm.view.activity.RegisterActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class LoginViewModel extends BaseViewModel<LoginModel> {
    public ObservableField<String> phone = new ObservableField<>();
    public ObservableField<String> pass = new ObservableField<>();

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void update(UserEventEntity entity){
        if (entity.action.equals("update")){
            phone.set(entity.phone);
            pass.set(entity.pwd);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void finish(){
        getUiChange().getFinishActObServAble().setValue(""+System.currentTimeMillis());
    }

    public void login(){
        if (phone.get().isEmpty()){
            ToastUtils.showShort("请输入手机号");
            return;
        }
        if (pass.get().isEmpty()){
            ToastUtils.showShort("请输入密码");
            return;
        }
        HashMap<String,Object> map = new HashMap<>();
        map.put("mobile",phone.get());
        map.put("pushId","1");
        map.put("pwd",pass.get());
        m.reuqestLoginModel(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);


    }

//13761646872
//    密码：0147

    @Override
    protected void result(BaseEntity baseEntity) {
        if (baseEntity instanceof UserEntity){
            UserEntity userEntity = (UserEntity) baseEntity;
            if (userEntity.getStatus() == -1)
                return;
            if (userEntity.getMessage().equals("用户不存在"))
                return;
            new UserDaoEntity().update(userEntity.data);
            UserEventEntity userEventEntity = new UserEventEntity();
            userEventEntity.action = "updateUser";
            EventBus.getDefault().post(userEventEntity);
            finish();
        }
    }

    public void forget(){
        HashMap<String,Object> map = new HashMap<>();
        map.put(UIFiled.PAGECLAZZ, ForgetActivity.class);
        getUiChange().getStartPageObServAble().setValue(map);
    }

    public void register(){
        HashMap<String,Object> map= new HashMap<>();
        map.put(UIFiled.PAGECLAZZ, RegisterActivity.class);
        getUiChange().getStartPageObServAble().setValue(map);
    }
}
